package io.zhifei.modules.sys.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.zhifei.common.utils.PageUtils;
import io.zhifei.common.utils.ToolUtils;
import io.zhifei.modules.sys.system.dao.CourseAuditDao;
import io.zhifei.modules.sys.system.entity.CourseAuditEntity;
import io.zhifei.modules.sys.system.form.CourseAuditParam;
import io.zhifei.modules.sys.system.form.CourseAuditResult;
import io.zhifei.modules.sys.system.service.CourseAuditService;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程审核 服务实现类
 * </p>
 *
 * @author duke
 * @since 2020-12-16
 */
@Service
public class CourseAuditServiceImpl extends ServiceImpl<CourseAuditDao, CourseAuditEntity> implements CourseAuditService {

    @Override
    public void add(CourseAuditParam param) {
        CourseAuditEntity entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CourseAuditParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(CourseAuditParam param) {
        CourseAuditEntity oldEntity = getOldEntity(param);
        CourseAuditEntity newEntity = getEntity(param);
        ToolUtils.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CourseAuditResult findBySpec(CourseAuditParam param) {
        return null;
    }

    @Override
    public PageUtils getPageList(Page page, CourseAuditParam param) {
        IPage<CourseAuditResult> pageList = this.baseMapper.getPageList(page,param);
        return new PageUtils(pageList);
    }

    @Override
    public List<Map> getSingeCourseList(CourseAuditParam param) {
        return this.baseMapper.getSingeCourseList(param);
    }


    private Serializable getKey(CourseAuditParam param) {
        return param.getId();
    }

    private CourseAuditEntity getOldEntity(CourseAuditParam param) {
        return this.getById(getKey(param));
    }

    private CourseAuditEntity getEntity(CourseAuditParam param) {
        CourseAuditEntity entity = new CourseAuditEntity();
        ToolUtils.copyProperties(param, entity);
        return entity;
    }

}
